<?php
require 'header.php';

$itemsToday=0;
$itemsWeek=0;
$itemsMonth=0;
$incomeToday=0;
$incomeWeek=0;
$incomeMonth=0;
$incomeToday=0;
$incomeWeek=0;
$incomeMonth=0;
$onlineItemsToday=0;
$onlineItemsWeek=0;
$onlineItemsMonth=0;
$onlineIncomeToday=0;
$onlineIncomeWeek=0;
$onlineIncomeMonth=0;
$downtownItemsToday=0;
$downtownItemsWeek=0;
$downtownItemsMonth=0;
$uptownItemsToday=0;
$uptownItemsWeek=0;
$uptownItemsMonth=0;
$downtownIncomeToday=0;
$downtownIncomeWeek=0;
$downtownIncomeMonth=0;
$uptownIncomeToday=0;
$uptownIncomeWeek=0;
$uptownIncomeMonth=0;

$itemsYesterday=0;
$itemsLastWeek=0;
$itemsLastMonth=0;
$incomeYesterday=0;
$incomeLastWeek=0;
$incomeLastMonth=0;
$incomeYesterday=0;
$incomeLastWeek=0;
$incomeLastMonth=0;
$onlineItemsYesterday=0;
$onlineItemsLastWeek=0;
$onlineItemsLastMonth=0;
$onlineIncomeYesterday=0;
$onlineIncomeLastWeek=0;
$onlineIncomeLastMonth=0;
$downtownItemsYesterday=0;
$downtownItemsLastWeek=0;
$downtownItemsLastMonth=0;
$uptownItemsYesterday=0;
$uptownItemsLastWeek=0;
$uptownItemsLastMonth=0;
$downtownIncomeYesterday=0;
$downtownIncomeLastWeek=0;
$downtownIncomeLastMonth=0;
$uptownIncomeYesterday=0;
$uptownIncomeLastWeek=0;
$uptownIncomeLastMonth=0;




$today=mktime(0, 0, 0,date('m'), date('d'), date('Y')); 
$week=mktime(0, 0, 0,date('m'), date('d')-date('w'), date('Y')); 
$month=mktime(0, 0, 0,date('m'), 01, date('Y')); 
$lastMonth=mktime(0, 0, 0,date('m')-1, 01, date('Y')); 
$yesterday=mktime(0, 0, 0,date('m'), date('d')-1, date('Y')); 
$lastWeek=mktime(0, 0, 0,date('m'), date('d')-date('w')-7, date('Y')); 

$sql="SELECT * FROM purchase_history";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result))
{
	////////////////////////////// Yesterdays's Statistics ////////////////////////
	if ((strtotime($row['date']) > $yesterday) && (strtotime($row['date']) < $today))
	{
		$itemsYesterday+=$row['quantity'];
		$incomeYesterday+=$row['price'];
		if ($row['branch'] =='online')
		{
			$onlineItemsYesterday+=$row['quantity'];
			$onlineIncomeYesterday+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$uptownItemsYesterday+=$row['quantity'];
			$uptownIncomeYesterday+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$downtownItemsYesterday+=$row['quantity'];
			$downtownIncomeYesterday+=$row['price'];
		}
	}
	///////////////////////////////////////////////////////////////////////////////
	
	
	////////////////////////////// Today's Statistics ////////////////////////
 	if (strtotime($row['date']) > $today)
	{
		$itemsToday+=$row['quantity'];
		$incomeToday+=$row['price'];
		if ($row['branch'] =='online')
		{
			$onlineItemsToday+=$row['quantity'];
			$onlineIncomeToday+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$uptownItemsToday+=$row['quantity'];
			$uptownIncomeToday+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$downtownItemsToday+=$row['quantity'];
			$downtownIncomeToday+=$row['price'];
		}
	} 
	///////////////////////////////////////////////////////////////////////////////
	
	////////////////////////////// Last Week's Statistics ////////////////////////
 	if ((strtotime($row['date']) < $week) && (strtotime($row['date'])> $lastWeek))
	{
		$itemsLastWeek+=$row['quantity'];
		$incomeLastWeek+=$row['price'];
		if ($row['branch'] =='online')
		{
			$onlineItemsLastWeek+=$row['quantity'];
			$onlineIncomeLastWeek+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$uptownItemsLastWeek+=$row['quantity'];
			$uptownIncomeLastWeek+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$downtownItemsLastWeek+=$row['quantity'];
			$downtownIncomeLastWeek+=$row['price'];
		}
	} 
	//////////////////////////////////////////////////////////////////////////
	
	////////////////////////////// This Week's Statistics ////////////////////////
	if (strtotime($row['date']) > $week)
	{
		$itemsWeek+=$row['quantity'];
		$incomeWeek+=$row['price'];
		if ($row['branch'] =='online')
		{
			$onlineItemsWeek+=$row['quantity'];
			$onlineIncomeWeek+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$uptownItemsWeek+=$row['quantity'];
			$uptownIncomeWeek+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$downtownItemsWeek+=$row['quantity'];
			$downtownIncomeWeek+=$row['price'];
		} 
	} 
	//////////////////////////////////////////////////////////////////////////
	
	
	////////////////////////////// Last Month's Statistics ////////////////////////
	if ((strtotime($row['date']) < $month) && (strtotime($row['date']) > $lastMonth))
	{
		$itemsLastMonth+=$row['quantity'];
		$incomeLastMonth+=$row['price'];
		if ($row['branch'] =='online')
		{
			$onlineItemsLastMonth+=$row['quantity'];
			$onlineIncomeLastMonth+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$uptownItemsLastMonth+=$row['quantity'];
			$uptownIncomeLastMonth+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$downtownItemsLastMonth+=$row['quantity'];
			$downtownIncomeLastMonth+=$row['price'];
		}
	}
	//////////////////////////////////////////////////////////////////////////

	////////////////////////////// This Month's Statistics ////////////////////////
 	if (strtotime($row['date']) > $month)
	{
		$itemsMonth+=$row['quantity'];
		$incomeMonth+=$row['price'];
		if ($row['branch'] =='online')
		{
			$onlineItemsMonth+=$row['quantity'];
			$onlineIncomeMonth+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$uptownItemsMonth+=$row['quantity'];
			$uptownIncomeMonth+=$row['price'];
		}
		if ($row['branch'] =='Uptown')
		{
			$downtownItemsMonth+=$row['quantity'];
			$downtownIncomeMonth+=$row['price'];
		}
	} 
	//////////////////////////////////////////////////////////////////////////
	
}


echo "<table border=1 cellpadding=10>";
echo "<tr><th> </th>
	<th>Today</th>
	<th>Yesterday</th>
	<th>This Week</th>
	<th>Last Week</th>
	<th>This Month</th>
	<th>Last Month</th></tr>";
echo "<tr><td>Items Sold</td>
	<td>$itemsToday</td>
	<td>$itemsYesterday</td>
	<td>$itemsWeek</td>
	<td>$itemsLastWeek</td>
	<td>$itemsMonth</td>
	<td>$itemsLastMonth</td></tr>";
echo "<tr><td>Total Income</td>
	<td>$".sprintf('%0.2f', $incomeToday)."</td>
	<td>$".sprintf('%0.2f', $incomeYesterday)."</td>
	<td>$".sprintf('%0.2f', $incomeWeek)."</td>
	<td>$".sprintf('%0.2f', $incomeLastWeek)."</td>
	<td>$".sprintf('%0.2f', $incomeMonth)."</td>
	<td>$".sprintf('%0.2f', $incomeLastMonth)."</td></tr>";
echo "<tr><td>Items Sold Online</td>
	<td>$onlineItemsToday</td>
	<td>$onlineItemsYesterday</td>
	<td>$onlineItemsWeek</td>
	<td>$onlineItemsLastWeek</td>
	<td>$onlineItemsMonth</td>
	<td>$onlineItemsLastMonth</td></tr>";
echo "<tr><td>Total Income Online</td>
	<td>$".sprintf('%0.2f', $onlineIncomeToday)."</td>
	<td>$".sprintf('%0.2f', $onlineIncomeYesterday)."</td>
	<td>$".sprintf('%0.2f', $onlineIncomeWeek)."</td>
	<td>$".sprintf('%0.2f', $onlineIncomeLastWeek)."</td>
	<td>$".sprintf('%0.2f', $onlineIncomeMonth)."</td>
	<td>$".sprintf('%0.2f', $onlineIncomeLastMonth)."</td></tr>";
echo "<tr><td>Items Sold Downtown</td>
	<td>$downtownItemsToday</td>
	<td>$downtownItemsYesterday</td>
	<td>$downtownItemsWeek</td>
	<td>$downtownItemsLastWeek</td>
	<td>$downtownItemsMonth</td>
	<td>$downtownItemsLastMonth</td></tr>";
echo "<tr><td>Total Income Downtown</td>
	<td>$".sprintf('%0.2f', $downtownIncomeToday)."</td>
	<td>$".sprintf('%0.2f', $downtownIncomeYesterday)."</td>
	<td>$".sprintf('%0.2f', $downtownIncomeWeek)."</td>
	<td>$".sprintf('%0.2f', $downtownIncomeLastWeek)."</td>
	<td>$".sprintf('%0.2f', $downtownIncomeMonth)."</td>
	<td>$".sprintf('%0.2f', $downtownIncomeLastMonth)."</td></tr>";
echo "<tr><td>Items Sold Uptown</td>
	<td>$uptownItemsToday</td>
	<td>$uptownItemsYesterday</td>
	<td>$uptownItemsWeek</td>
	<td>$uptownItemsLastWeek</td>
	<td>$uptownItemsMonth</td>
	<td>$uptownItemsLastMonth</td></tr>";
echo "<tr><td>Total Income Uptown</td>
	<td>$".sprintf('%0.2f', $uptownIncomeToday)."</td>
	<td>$".sprintf('%0.2f', $uptownIncomeYesterday)."</td>
	<td>$".sprintf('%0.2f', $uptownIncomeWeek)."</td>
	<td>$".sprintf('%0.2f', $uptownIncomeLastWeek)."</td>
	<td>$".sprintf('%0.2f', $uptownIncomeMonth)."</td>
	<td>$".sprintf('%0.2f', $uptownIncomeLastMonth)."</td></tr>";

echo "</table>";

require 'footer.php';
?>